package com.sihai.ehr.web.action.ajax;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.sihai.ehr.model.biz.InviteApply;
import com.sihai.ehr.service.InviteService;
import com.sihai.ehr.web.pojo.InviteApplyPojo;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.Duty;
import com.sihai.ehr.model.biz.Grade;
import com.sihai.ehr.model.biz.IdentificationCard;
import com.sihai.ehr.model.biz.InterviewArrange;
import com.sihai.ehr.model.biz.InviteArrange;
import com.sihai.ehr.model.biz.InviteFashion;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.DutyService;
import com.sihai.ehr.service.GradeService;
import com.sihai.ehr.service.InterviewService;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.web.action.EHRAction;

/**
 * 招聘管理
 * 
 * @author Henry-PC
 */
public class InviteJsonControl extends EHRAction {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -3203576448907290662L;

	@BeanInject
	private OrganizationService organizationService;

	@BeanInject
	private DutyService dutyService;

	@BeanInject
	private GradeService gradeService;

	@BeanInject
	private InviteService inviteService;

	@BeanInject
	private InterviewService interviewService;

	private Long id;

	private String orgs;
	/** 员工编号 */
	private Long employeeId;
	/** 面试安排编号 */
	private Long interviewArrangeId;

	private List<Organization> organizations;

	private List<Duty> duties;

	private List<Grade> grades;

	private List<InviteApplyPojo> inviteApplyPojos;

	/**
	 * 招聘渠道
	 */
	private InviteFashion inviteFashion;

	/** 招聘安排列表 */
	private List<InviteArrange> inviteArranges;

	/**
	 * 获取组织架构序列数
	 * 
	 * @return
	 */
	@Action(value = "/ajax/org_list", results = { @Result(name = "success", type = "json", params = { "root", "organizations" }) })
	public String getOrganizationListById() {
		try {
			organizations = organizationService.findChildren(id, true, null, null);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			;
		}
		return SUCCESS;
	}

	/**
	 * 根据组织编号查询招聘申请列表
	 * 
	 * @return
	 */
	@Action(value = "/ajax/apply_list", results = { @Result(name = "success", type = "json", params = { "root", "inviteApplyPojos" }) })
	public String queryInviteApplyListByOrgId() {
		try {
			inviteApplyPojos = inviteService.searchInviteAppliesTOPOJO(organizationId, 0L, null, null);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 处理识别卡产生时间
	 * 
	 * @return
	 */
	@Action(value = "/ajax/generate_cardtime", results = { @Result(name = "success", type = "plainText") })
	public String handleIdentityCardGenerateTime() {
		try {
			IdentificationCard mCard = interviewService.queryIdentityCardByEmployeeId(employeeId, interviewArrangeId);
			if (mCard == null && interviewArrangeId > 0) {
				mCard = new IdentificationCard();
				InterviewArrange mInterviewArrange = interviewService.getInterviewArrange(interviewArrangeId);
				mCard.setInterviewArrange(mInterviewArrange);
				mCard.setEmployeeinfoBase(mInterviewArrange.getEmployeeinfoBase());
			}
			// 检测是否为空
			if (mCard != null) {
				mCard.setDentificationCardTime(new Date(System.currentTimeMillis()));
				interviewService.saveIdentificationCard(mCard);
				HttpServletResponse response = ServletActionContext.getResponse();
				response.getWriter().write("OK");
				response.getWriter().flush();
				response.getWriter().close();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

		return SUCCESS;
	}

	/**
	 * 获取序列化组织关系
	 * 
	 * @return
	 */
	@Action(value = "/ajax/org_serlist", results = { @Result(name = "success", type = "plainText") })
	public String getserializableOrganization() {
		try {
			String jsonOrgList = serializableOrganization("", organizationService.findChildren(null, false, null, null));
			HttpServletResponse response = ServletActionContext.getResponse();
			response.getWriter().write(jsonOrgList);
			response.getWriter().flush();
			response.getWriter().close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 序列化组织架构
	 * 
	 * @param listOrganizations
	 * @return
	 */
	private String serializableOrganization(String parentOrgName, List<Organization> listOrganizations) {
		StringBuffer sb = new StringBuffer("[");
		if (listOrganizations != null && listOrganizations.size() > 0) {
			for (int i = 0; i < listOrganizations.size(); i++) {
				Organization organization = listOrganizations.get(i);
				if (i > 0) {
					sb.append(",");
				}
				sb.append("{");
				sb.append("\"id\":").append(organization.getId());
				sb.append(",\"name\":").append("\"" + (parentOrgName.isEmpty() ? "" : parentOrgName + "/") + organization.getFullName() + "\"");
				sb.append(",\"children\":");
				sb.append(serializableOrganization((parentOrgName.isEmpty() ? "" : parentOrgName + "/") + organization.getFullName(),
						organizationService.findChildren(organization.getId(), true, null, null)));
				sb.append("}");
			}
		}
		sb.append("]");
		return sb.toString();
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getOrgs() {
		return orgs;
	}

	public void setOrgs(String orgs) {
		this.orgs = orgs;
	}

	public void setEmployeeId(Long employeeId) {
		this.employeeId = employeeId;
	}

	public Long getEmployeeId() {
		return employeeId;
	}

	public void setInterviewArrangeId(Long interviewArrangeId) {
		this.interviewArrangeId = interviewArrangeId;
	}

	public Long getInterviewArrangeId() {
		return interviewArrangeId;
	}

	public List<Organization> getOrganizations() {
		return organizations;
	}

	public void setOrganizations(List<Organization> organizations) {
		this.organizations = organizations;
	}

	public List<Duty> getDuties() {
		return duties;
	}

	public void setDuties(List<Duty> duties) {
		this.duties = duties;
	}

	public List<Grade> getGrades() {
		return grades;
	}

	public void setGrades(List<Grade> grades) {
		this.grades = grades;
	}

	public List<InviteApplyPojo> getInviteApplyPojos() {
		return inviteApplyPojos;
	}

	public void setInviteApplyPojos(List<InviteApplyPojo> inviteApplyPojos) {
		this.inviteApplyPojos = inviteApplyPojos;
	}

	public InviteFashion getInviteFashion() {
		return inviteFashion;
	}

	public void setInviteFashion(InviteFashion inviteFashion) {
		this.inviteFashion = inviteFashion;
	}

	public List<InviteArrange> getInviteArranges() {
		return inviteArranges;
	}

	public void setInviteArranges(List<InviteArrange> inviteArranges) {
		this.inviteArranges = inviteArranges;
	}
}
